#   Copyright (c) 2008 ToI-Planning <office@toi-planning.net>
# 
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
# 
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#   3. Neither the name of the authors nor the names of its contributors
#      may be used to endorse or promote products derived from this
#      software without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
#   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  $Id: ContextHandlers.pm 2 2008-09-16 16:41:51Z usualoma $

package StyledFields::Template::ContextHandlers;

use strict;

sub _hdlr_include_css_hashs {
    my($ctx, $args) = @_;

	require MT;
	my $app = MT->instance;
	my $blog_id = $app->param('blog_id') || $app->param('id') || return;

	my $plugin = $app->component('StyledFields');

	my $css_hash = $plugin->get_config_value(
		'css_hash', 'blog:' . $blog_id
	) || {};

	require CustomFields::Field;

	my (%css_hashs, %css_hashs_page);
	my @fields = CustomFields::Field->load({
		'blog_id' => [0, $blog_id],
	});
	foreach my $f (@fields) {
		my $hash = {};
        my $id = $f->id;
		$hash->{'key'} = $id;
		$hash->{'name'} = $f->name;
		$hash->{'css'} = $css_hash->{$id} || '';

		if ($f->obj_type eq 'entry') {
			$css_hashs{$id} = $hash;
		}
		elsif ($f->obj_type eq 'page') {
			$css_hashs_page{$id} = $hash;
		}
	}

    $ctx->var('css_hashs', \%css_hashs);
    $ctx->var('css_hashs_page', \%css_hashs_page);
}

1;
